package com.start.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.admin.entity.StartSysMenuDataRules;
import com.start.admin.service.StartSysMenuDataRulesService;
import com.start.common.api.Result;
import com.start.common.dataRule.DataRuleConditionEnum;
import com.start.common.tools.StartEnumTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/start/sysDataRule")
//@RegisterMenu(value = "数据源管理", systemCode = "codeGenerator:DataSource", component = "views/codeGenerator/datasource/index", path = "/codeGenerator/datasource/index")
public class StartSysDataRuleController {
    @Autowired
    private StartSysMenuDataRulesService menuDataRulesService;

    @GetMapping("/page")
    @PreAuthorize("@pas.hasPermission('start:sys:dataRule:list')")
    public Result<?> page(Page page, StartSysMenuDataRules entity) {
        LambdaQueryWrapper<StartSysMenuDataRules> lambdaQuery = Wrappers.lambdaQuery(entity);
        return Result.ok(menuDataRulesService.page(page, lambdaQuery));
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('start:sys:dataRule:add')")
    public Result<?> add(@RequestBody StartSysMenuDataRules entity) {
        menuDataRulesService.save(entity);
        return Result.ok();
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('start:sys:dataRule:edit')")
    public Result<?> updateById(@RequestBody StartSysMenuDataRules entity) {
        menuDataRulesService.updateById(entity);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('start:sys:dataRule:info')")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(menuDataRulesService.getById(id));
    }


    @DeleteMapping("deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:sys:dataRule:delete')")
    public Result<?> deleteById(@PathVariable("id") String id) {
        menuDataRulesService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/getRuleConditions")
    public Result<?> getRuleConditions() {
        return Result.ok( StartEnumTools.getEnumDic(DataRuleConditionEnum.class));
    }
    @GetMapping("/getDataRulesByMenuId/{menuId}")
    @PreAuthorize("@pas.hasPermission('start:sys:dataRule:list')")
    public Result<?> getDataRulesByMenuId(@PathVariable("menuId") String menuId){
        List<StartSysMenuDataRules> list=menuDataRulesService.listByMenuId(menuId);
        return Result.ok(list);
    }

}
